﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Graphics;
using Android.OS;
using Android.Support.Design.Widget;
using Android.Support.V4.App;
using Android.Support.V4.View;
using Android.Support.V4.Widget;
using Android.Support.V7.App;
using Android.Support.V7.Widget;
using Android.Views;
using Android.Widget;
using Com.Gcssloop.Widget;
using FFImageLoading;
using FFImageLoading.Views;
using mb;
using mb.Droid;
using static Android.Support.V7.Widget.GridLayoutManager;

namespace libmp
{
    class ExMovieList : BaseMixList<DataStore.VideoItem>
    {

        protected override View CreateCell(Dictionary<string, object> viewData)
        {
            // var view = new View(activity);
            var squareLayout = new MySquareLayout(activity, 2 * 0.5f);
            // view.SetBackgroundColor(Color.Green);
            ViewGroup rcl;
            bool useRC = false;
            if (useRC)
            {
                var rclReal = new RCRelativeLayout(activity);
                rclReal.SetRadius(30);
                rclReal.ClipBackground = true;
                rclReal.StrokeColor = Color.Black;
                rclReal.StrokeWidth = 2;
                rcl = rclReal;

                var lp = new FrameLayout.LayoutParams(-1, -1);
                lp.SetMargins(20, 20, 20, 20);
                squareLayout.AddView(rcl, lp);
            }
            else
            {
                var fl = new FrameLayout(activity);
                var lp = new FrameLayout.LayoutParams(-1, -1);
                lp.SetMargins(20, 20, 20, 20);
                rcl = fl;
                squareLayout.AddView(fl, lp);
            }

            var oneItemLayout = new LinearLayout(activity);
            oneItemLayout.Orientation = Orientation.Vertical;
            oneItemLayout.LayoutParameters = new LinearLayout.LayoutParams(-1, -1);
            rcl.AddView(oneItemLayout);

            var imageViewLayout = new LinearLayout(activity);
            imageViewLayout.Orientation = Orientation.Vertical;
            imageViewLayout.LayoutParameters = new LinearLayout.LayoutParams(-1, 0, 7);
            oneItemLayout.AddView(imageViewLayout);

            var imageView = new ImageViewAsync(activity);
            imageView.SetScaleType(ImageView.ScaleType.CenterCrop);
            var imageViewParams = new LinearLayout.LayoutParams(-1, -1);
            imageView.LayoutParameters = imageViewParams;
            imageViewLayout.AddView(imageView);
            viewData.Add("imageView", imageView);

            // var topLayoutAlpha = new LinearLayout(activity);
            // imageViewLayout.AddView(topLayoutAlpha);
            // var topLayoutAlphaParams = new LinearLayout.LayoutParams(-1, 80);
            // topLayoutAlpha.SetBackgroundColor(Color.Black);
            // topLayoutAlpha.Alpha = 0.5f;
            // topLayoutAlphaParams.TopMargin = -80;
            // topLayoutAlpha.LayoutParameters = topLayoutAlphaParams;

            var topLayout = new LinearLayout(activity);
            imageViewLayout.AddView(topLayout);
            topLayout.Orientation = Orientation.Horizontal;
            var topLayoutParams = new LinearLayout.LayoutParams(-1, 80);
            topLayout.SetBackgroundColor(Color.Black);
            topLayout.Alpha = 0.5f;
            topLayoutParams.TopMargin = -80;
            topLayout.LayoutParameters = topLayoutParams;

            var topLayoutLeft = new LinearLayout(activity);
            topLayout.AddView(topLayoutLeft);
            topLayoutLeft.Orientation = Orientation.Horizontal;
            topLayoutLeft.LayoutParameters = new LinearLayout.LayoutParams(0, -1, 1);

            var leftImage = new ImageView(activity);
            leftImage.SetImageDrawable(Util.DrawableFromFile("category/Bofang01.png"));
            var leftImageParams = new LinearLayout.LayoutParams(50, 50);
            leftImageParams.LeftMargin = 15;
            leftImageParams.TopMargin = 15;
            leftImage.LayoutParameters = leftImageParams;
            topLayoutLeft.AddView(leftImage);

            var leftTextView = new TextView(activity);
            var leftTextViewParameters = new LinearLayout.LayoutParams(-2, -1);
            leftTextViewParameters.LeftMargin = 10;
            leftTextView.LayoutParameters = leftTextViewParameters;
            leftTextView.Gravity = GravityFlags.CenterVertical;
            leftTextView.SetTextColor(Color.White);
            topLayoutLeft.AddView(leftTextView);
            viewData.Add("bofangText", leftTextView);

            var topLayoutRight = new LinearLayout(activity);
            topLayout.AddView(topLayoutRight);
            topLayoutRight.SetGravity(GravityFlags.Right);
            topLayoutRight.Orientation = Orientation.Horizontal;
            topLayoutRight.LayoutParameters = new LinearLayout.LayoutParams(0, -1, 1);

            var rightTextView = new TextView(activity);
            var rightTextViewParameters = new LinearLayout.LayoutParams(-2, -1);
            rightTextViewParameters.RightMargin = 10;
            rightTextView.LayoutParameters = rightTextViewParameters;
            rightTextView.Gravity = GravityFlags.CenterVertical;
            rightTextView.SetTextColor(Color.White);
            topLayoutRight.AddView(rightTextView);
            viewData.Add("dianzanText", rightTextView);

            var rightImage = new ImageView(activity);
            rightImage.SetImageDrawable(Util.DrawableFromFile("category/Dianzan01.png"));
            var rightImageParams = new LinearLayout.LayoutParams(50, 50);
            rightImageParams.RightMargin = 15;
            rightImageParams.TopMargin = 15;
            rightImage.LayoutParameters = rightImageParams;
            topLayoutRight.AddView(rightImage);

            var tv = new TextView(activity);
            var tvLayoutParameters = new LinearLayout.LayoutParams(-1, 0, 3);
            tvLayoutParameters.LeftMargin = 15;
            tvLayoutParameters.RightMargin = 15;
            tv.LayoutParameters = tvLayoutParameters;
            tv.Gravity = GravityFlags.CenterVertical;
            tv.SetLines(2);
            oneItemLayout.AddView(tv);
            viewData.Add("text", tv);

            squareLayout.Click += (s, e) =>
            {
                int p = this.recyclerView.GetChildAdapterPosition(squareLayout);
                int idx = GetDataIndex(p);
                SelectItem(idx);
            };

            return squareLayout;
        }

        protected override void UpdateCell(View cell, int p, Dictionary<string, object> viewData)
        {
            int idx = GetDataIndex(p);
            logger.Log("updatecell", p, idx);
            if (idx == -1) return;

            var tv = viewData["text"] as TextView;
            tv.Text = dataSource[idx].vname;
            var imageView = viewData["imageView"] as ImageViewAsync;
            FFImageLoading.ImageService.Instance.LoadUrl(dataSource[idx].vpic).Into(imageView);
            var bofangText = viewData["bofangText"] as TextView;
            bofangText.Text = dataSource[idx].views + "";
            var dianzanText = viewData["dianzanText"] as TextView;
            dianzanText.Text = dataSource[idx].praises + "";
        }

        protected virtual void SelectItem(int idx)
        {
            MoviePlayView.OpenPlayView(dataSource[idx]);
        }
    }
}